package com.example.hospital.controller;

import com.example.hospital.dto.AppointmentQuery;
import com.example.hospital.dto.Result;
import com.example.hospital.entity.Appointment;
import com.example.hospital.service.AppointmentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("appointment")
@Tag(name = "Appointment", description = "预约相关接口")
public class AppointmentController {
    @Resource
    private AppointmentService appointmentService;

    /**
     * 新增预约
     *
     * @param appointment
     * @return appointment id or error
     */
    @Operation(
            summary = "新增预约",
            description = "创建一个新的预约记录",
            responses = {
                    @ApiResponse(responseCode = "200", description = "添加成功, 返回预约id"),
                    @ApiResponse(responseCode = "400", description = "请求参数错误")
            }
    )
    @PostMapping
    public Result<Integer> addAppointment(@RequestBody Appointment appointment) {
        boolean saved = appointmentService.save(appointment);
        return saved ? Result.success(appointment.getAppointmentId()) : Result.fail("添加失败");
    }

    /**
     * 根据id删除预约
     *
     * @param id
     * @return
     */
    @Operation(
            summary = "根据id删除预约",
            description = "通过预约ID删除指定的预约记录",
            responses = {
                    @ApiResponse(responseCode = "200", description = "删除成功"),
                    @ApiResponse(responseCode = "400", description = "预约未找到")
            }
    )
    @DeleteMapping("/{id}")
    public Result deleteAppointment(@PathVariable("id") Integer id) {
        boolean removed = appointmentService.removeById(id);
        return removed ? Result.success("删除成功") : Result.fail("删除失败");
    }

    /**
     * 修改预约信息
     *
     * @param appointment
     * @return
     */
    @Operation(
            summary = "修改预约信息",
            description = "通过传递新的预约信息更新现有预约",
            responses = {
                    @ApiResponse(responseCode = "200", description = "修改成功"),
                    @ApiResponse(responseCode = "400", description = "请求参数错误")
            }
    )
    @PutMapping
    public Result updateAppointment(@RequestBody Appointment appointment) {
        boolean updated = appointmentService.updateById(appointment);
        return updated ? Result.success() : Result.fail("修改失败");
    }

    /**
     * 根据id查询预约
     *
     * @param id
     * @return appointment or error
     */
    @Operation(
            summary = "根据id查询预约",
            description = "根据预约ID查询预约信息",
            responses = {
                    @ApiResponse(responseCode = "200", description = "查询成功"),
                    @ApiResponse(responseCode = "400", description = "预约未找到")
            }
    )
    @GetMapping("/{id}")
    public Result<Appointment> getAppointment(@PathVariable("id") Integer id) {
        Appointment appointment = appointmentService.getById(id);
        return appointment != null ? Result.success(appointment) : Result.fail("不存在该预约");
    }

    /**
     * 查询所有预约
     *
     * @return appointment list
     */
    @Operation(
            summary = "查询所有预约",
            description = "获取所有预约记录",
            responses = {
                    @ApiResponse(responseCode = "200", description = "查询成功"),
            }
    )
    @GetMapping
    public Result<List<Appointment>> listAppointments() {
        return Result.success(appointmentService.list());
    }

    /**
     * 条件查询预约
     *
     * @param appointmentQuery
     * @return appointment list
     */
    @Operation(
            summary = "条件查询预约",
            description = "根据条件查询预约记录",
            responses = {
                    @ApiResponse(responseCode = "200", description = "查询成功"),
                    @ApiResponse(responseCode = "400", description = "请求参数错误")
            }
    )
    @PostMapping("query")
    public Result<List<Appointment>> listAppointments(@RequestBody AppointmentQuery appointmentQuery) {
        List<Appointment> appointments = appointmentService.listAppointments(appointmentQuery);
        return Result.success(appointments);
    }
}
